#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import render_template, redirect, url_for, abort, flash, request, \
    current_app, make_response
from flask.ext.login import login_required, current_user
from . import hosts
from .. import db
from ..models import User, Host
from .forms import HostForm
from .. lib import util


@hosts.route("/hostadd", methods=['GET', 'POST'])
def add_host():
    form = HostForm()
    if form.validate_on_submit():
        host = Host(
            hostname=form.hostname.data,
            IP=form.IP.data,
            OS=form.OS.data,
            sshport=form.sshport.data,
            loginUser=form.loginUser.data,
            loginPass=form.loginPass.data,
        )
        sshclient = util.SSHClient(
            host.IP, host.loginUser, password=host.loginPass, port=host.sshport)
        if not sshclient.verifyLogin():
            newform = HostForm()
            newform.hostname.data = host.hostname
            newform.IP.data = host.IP
            newform.OS.data = host.OS
            newform.sshport.data = host.sshport
            newform.loginUser.data = host.loginUser
            newform.loginPass.data = host.loginPass
            newform.loginPass_verfiy.data = host.loginPass
            message = "LoginUser LoginPass can not login this host"
            flash(message)
            return render_template('hosts/addhost.html', form=newform)

        db.session.add(host)
        db.session.commit()
        #flash('A host was added!')
        return redirect(url_for('application.index'))
    return render_template('hosts/addhost.html', form=form)


@hosts.route("/edit/<int:id>", methods=['GET', 'POST'])
def edit_host(id):
    form = HostForm()
    host = Host.query.get_or_404(id)
    if form.validate_on_submit():

        host.hostname = form.hostname.data
        host.IP = form.IP.data
        host.OS = form.OS.data
        sshport = form.sshport.data
        loginUser = form.loginUser.data
        loginPass = form.loginPass.data

        sshclient = util.SSHClient(
            host.IP, host.loginUser, password=host.loginPass, port=host.sshport)
        if not sshclient.verifyLogin():
            newform = HostForm()
            newform.hostname.data = host.hostname
            newform.IP.data = host.IP
            newform.OS.data = host.OS
            newform.sshport.data = host.sshport
            newform.loginUser.data = host.loginUser
            newform.loginPass.data = host.loginPass
            newform.loginPass_verfiy.data = host.loginPass
            message = "LoginUser LoginPass can not login this host"
            flush(message)

            return render_template('hosts/addhost.html', form=newform)

        db.session.add(host)
        db.session.commit()
        #flash('A host was added!')
        return redirect(url_for('application.index'))
    form.hostname.data = host.hostname
    form.IP.data = host.IP
    form.OS.data = host.OS
    form.sshport.data = host.sshport
    form.loginUser.data = host.loginUser
    form.loginPass.data = host.loginPass
    form.loginPass_verfiy.data = host.loginPass
    return render_template('hosts/addhost.html', form=form)


@hosts.route('/hostlist')
def hostlist():
    hostlist = Host.query.all()
    return render_template('hosts/list.html', hostlist=hostlist)


@hosts.route("/view/<category>/<id>")
def view(category, id):
    pass


@hosts.route("/view/<category>/<id>")
def delete(category, id):
    pass
